<!DOCTYPE html>
<html>

<body>
    <p>Test Arpc Client</p>
    <p id="notify"></p>
    <p id="response"></p>

    <script src="./arpc.js" type="text/javascript"></script>
    <script type="text/javascript">
        var client = new ArpcClient("ws://localhost:8888/ws");
        client.onOpen = function () {
            console.log("ArpcClient onOpen");
        }
        client.onClose = function () {
            console.log("ArpcClient onClose");
        }
        client.handle("/server/notify", function (ctx) {
            console.log("[/server/notify]:", ctx.data);
            document.getElementById("notify").innerHTML = ctx.data;
        });
        var count = 0;
        var timeout = 1000;
        setInterval(function () {
            count++;
            // use promise
            client.call("/call/echo", `call ${count}`, timeout).then(function (reply) {
                console.log("[/call/echo]:", reply.data, reply.err);
                document.getElementById("response").innerHTML = `[/call/echo] reply: '${reply.data}', error: '${reply.err}'`;
            });
            // use callback
            // client.call("/call/echo", `call ${count}`, timeout, function (reply) {
            //     console.log("[/call/echo]:", reply.data, reply.err);
            //     document.getElementById("response").innerHTML = `[/call/echo] reply: '${reply.data}', error: '${reply.err}'`;
            // });
            client.notify("/notify", `notify ${count}`);
        }, 1000);
    </script>
</body>

</html>